package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.result.course.ChapterVo;
import com.atguigu.eduservice.entity.result.course.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 老王
 * @since 2020-05-20
 */
@Api(tags = "课程管理相关接口")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;


    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){

        boolean result = courseService.removeCourseById(id);
        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }


    @ApiOperation(value = "分页条件查询课程列表")
    @GetMapping("/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    CourseQuery courseQuery){

        Page<EduCourse> pageParam = new Page<>(page, limit);

        courseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> list = pageParam.getRecords();

        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("list", list);
    }




    @ApiOperation("添加课程信息")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody(required = false) CourseInfoVo courseInfoVo){
        String courseId=courseService.addCourseInfo(courseInfoVo);
        if (!StringUtils.isEmpty(courseId)){
            return R.ok().data("courseId", courseId);
        }else {
            return R.error();
        }
    }

    @ApiOperation("根据课程id查询课程章节和小节")
    @ApiImplicitParam(name="courseId",value = "课程id",required = true)
    @GetMapping("/getChapterOrVideo/{courseId}")
    public R getChapterOrVideo(@PathVariable String courseId){
        List<ChapterVo> list=courseService.getChapterOrVideo(courseId);
        return R.ok().data("list",list);
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping("{courseId}")
    public R getCourseInfoVoById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){

        CourseInfoVo courseInfo = courseService.getCourseInfoVoById(courseId);
        return R.ok().data("courseInfo", courseInfo);
    }

    @ApiOperation(value = "修改课程基础信息")
    @PostMapping("/updateCourse")
    public R updateCourse(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourse(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("/getCoursePublishInfo/{id}")
    public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){

        CoursePublishVo courseInfo = courseService.getCoursePublishVoById(id);
        return R.ok().data("courseInfo", courseInfo);
    }

    @ApiOperation(value = "根据id发布课程")
    @GetMapping("/publish/{id}")
    public R publishCourseById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){

        courseService.publishCourseById(id);
        return R.ok();
    }

}

